﻿namespace Amazon.Checkout.ViewModels
{
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using Orchard.Localization;

    public class PromotionNewUpdateViewModel : IValidatableObject
    {
        public PromotionNewUpdateViewModel() 
        {
            this.T = NullLocalizer.Instance;
        }
        
        public int PromotionId { get; set; }

        [Required]
        public string PromotionCode { get; set; }

        public string Description { get; set; }

        public double? DiscountRate { get; set; }

        public double? FixedAmountDiscount { get; set; }

        public string PromotionType { get; set; }

        public Localizer T { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (this.PromotionType == "FixedAmountDiscount")
            {
                if (!this.FixedAmountDiscount.HasValue)
                {
                    yield return new ValidationResult(this.T("Fixed Amount must be set.").Text);
                }

                if (this.FixedAmountDiscount <= 0)
                {
                    yield return new ValidationResult(this.T("Fixed Amount Discount must be greater than zero.").Text);
                }
            }
            else if (this.PromotionType == "DiscountRate")
            {
                if (!this.DiscountRate.HasValue)
                {
                    yield return new ValidationResult(this.T("Discount Rate must be set.").Text);
                }

                if (this.DiscountRate <= 0.0 || this.DiscountRate > 0.15)
                {
                    yield return new ValidationResult(this.T("The discount rate {0} is out of range. It should be greater than {1} and less than or equal to {2}.", this.DiscountRate, "0", "0.15").Text);
                }
            }
            else
            {
                yield return new ValidationResult(this.T("Promotion Type must be specified.").Text);
            }

            yield break;
        }
    }
}
